package com.platform.controller;

import java.util.List;
import java.util.Map;

import com.platform.entity.ShareBillEntity;
import com.platform.entity.SysUserEntity;
import com.platform.service.ShareBillService;
import com.platform.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.platform.entity.ApiShareBillEntity;
import com.platform.service.ApiShareBillService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

/**
 * Controller
 *
 * @author zhurusen
 * @email 524227263@qq.com
 * @date 2018-06-16 14:48:27
 */
@Controller
@RequestMapping("sharebill")
public class ShareBillController {
    @Autowired
    private ShareBillService shareBillService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sharebill:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ShareBillEntity> apiShareBillList = shareBillService.queryList(query);
        int total = shareBillService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(apiShareBillList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sharebill:info")
    @ResponseBody
    public R info(@PathVariable("id") Long id) {
        ShareBillEntity shareBill = shareBillService.queryObject(id);
        String channelName = "";
        if (shareBill != null) {
            SysUserEntity sysUserEntity = sysUserService.queryObject(shareBill.getChannel());
            if (sysUserEntity != null) {
                shareBill.setChannelName(sysUserEntity.getUsername());
            } else {
                shareBill.setChannelName("-");
            }

        }

        return R.ok().put("apiShareBill", shareBill);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sharebill:save")
    @ResponseBody
    public R save(@RequestBody ShareBillEntity apiShareBill) {
        shareBillService.save(apiShareBill);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sharebill:update")
    @ResponseBody
    public R update(@RequestBody ShareBillEntity apiShareBill) {
        shareBillService.update(apiShareBill);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sharebill:delete")
    @ResponseBody
    public R delete(@RequestBody Long[] ids) {
        shareBillService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ShareBillEntity> list = shareBillService.queryList(params);

        return R.ok().put("list", list);
    }
}
